﻿using DSFramework;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ToWangKeLib;

namespace ChaoXingGUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainView : Window
    {
        private static bool isDownload = false;//是否可以保存题目
        public MainView()
        {
            InitializeComponent();
            SQL.InitSQL("", @$"../../../db/WangKe.db", "", "", "SQLite");
            //SQL.InitSQL("", @$"WangKe.db", "", "", "SQLite");

            //SQL.Db.Queryable<WorkTitle>().ToList().ForEach(workTitle =>
            //{
            //    Console.WriteLine($"workTitle = {workTitle.WorkQuestion}");
            //});
        }

        private void Button_Click_Login(object sender, RoutedEventArgs e)
        {
            bool isLogin = ChaoXing.Login(txtAct.Text, txtPwd.Text);
            if (isLogin)
            {
                isDownload = true;
                MessageBox.Show("登录成功!");
            }
            else
            {
                isDownload = false;
                MessageBox.Show("登录失败!");
            }
        }

        private static List<WorkTitle> workTitles = new List<WorkTitle>();
        private void Button_Click_Get(object sender, RoutedEventArgs e)
        {
            if (isDownload)
            {
                var workList = ChaoXing.GetFinishCourse2(txtCourseName.Text, txtUrl.Text);
                foreach (var item in workList)
                {
                    WorkTitle workTitle = new WorkTitle()
                    {
                        Id = item.Id,
                        CourseName = item.CourseName,
                        WorkAnswer = item.WorkAnswer,
                        WorkQuestion = item.WorkQuestion,
                        WorkSelect = item.WorkSelect,
                    };
                    SQL.DB.Insertable<WorkTitle>(workTitle).ExecuteCommand();
                }
            }
            else
            {
                MessageBox.Show("未登录,无法保存!");
            }
        
        }

        public static string QuesTxt = "";
        string cpStr = "";
        private void Button_Click_WriteTitle(object sender, RoutedEventArgs e)
        {
            Task.Run(async () =>
            {
                while (true)
                {
                    App.Current.Dispatcher.Invoke(() =>
                    {
                        try
                        {
                            cpStr = Clipboard.GetText().Trim();
                            QuesTxt = cpStr;
                            txtAutoTitle.Text = cpStr;
                        }
                        catch { }
                    });
                    await Task.Delay(300);
                }
            });
        }

        private void txtAutoTitle_TextChanged(object sender, TextChangedEventArgs e)
        {
            string AnswerTxt = "";
            var answer = SQL.DB.Queryable<WorkTitle>().First(it => it.WorkQuestion.Contains(txtAutoTitle.Text));
            if (answer != null)
            {
                AnswerTxt += answer.WorkQuestion + "\r\n";
                AnswerTxt += answer.WorkSelect.Replace(";", "\r\n") + "\r\n";
                AnswerTxt += answer.WorkAnswer;

                txtAnswer.Text = AnswerTxt;

            }
        }

        private void Button_Click_SouSuo(object sender, RoutedEventArgs e)
        {
            string AnswerTxt = "";

            var answer = SQL.DB.Queryable<WorkTitle>().First(it => it.WorkQuestion.Contains(txtAutoTitle.Text));
            if (answer != null)
            {
                AnswerTxt += answer.WorkQuestion + "\r\n";
                AnswerTxt += answer.WorkSelect.Replace(";", "\r\n") + "\r\n";
                AnswerTxt += answer.WorkAnswer;

                txtAnswer.Text = AnswerTxt;

            }
        }
    }
}
